#!/bin/sh

# If you don't use a (Gnome) settings daemon, this will manually add some values to the dconf database
# (through gsettings)
cmd="$1"

do_cmd() {
    case $cmd in
        get) >&2 echo -n "$schema $key "; >&2 gsettings get "$schema" "$key"
        ;;
        set) >&2 gsettings set "$schema" "$key" "$val"
        ;;
        *)  >&2 echo "unknown command \"$cmd\" - should be either get or set"
            exit 1
        ;;
    esac
}

ini="${XDG_CONFIG_HOME-$HOME/.config}/gtk-3.0/settings.ini"

rex="gtk-theme-name|gtk-icon-theme-name|gtk-font-name|gtk-cursor-theme-name|gtk-cursor-theme-size"
schema="org.gnome.desktop.interface"
grep -E "^($rex)" "$ini" | while read -r line; do
    val="${line#*=}"
    key="${line%=*}"
    case "$key" in
        gtk-theme-name) 
            [[ "$cmd" == set ]] && echo "$val" # see wiki.archlinux.org/title/GTK#Themes
            key="gtk-theme" ;;
        gtk-icon-theme-name) key="icon-theme" ;;
        gtk-font-name) key="font-name";;
        gtk-cursor-theme-name) key="cursor-theme";;
        gtk-cursor-theme-size) key="cursor-size";;
        gtk-enable-event-sounds) key="event-sounds";schema="org.gnome.desktop.sound";;
        gtk-enable-input-feedback-sounds) key="input-feedback-sounds";schema="org.gnome.desktop.sound";;
    esac
    [[ "$key" != "" && "$val" != "" ]] && {
        [[ "$val" =~ ^[0-9]+$ ]] || val="'$val'"
    }
    do_cmd
done

rex="gtk-enable-event-sounds|gtk-enable-input-feedback-sounds"
schema="org.gnome.desktop.sound"
grep -E "^($rex)" "$ini" | while read -r line; do
    val="${line#*=}"
    key="${line%=*}"
    case "$key" in
        gtk-enable-event-sounds) key="event-sounds";;
        gtk-enable-input-feedback-sounds) key="input-feedback-sounds";;
    esac
    [[ "$key" != "" && "$val" != "" ]] && {
        [[ "$val" =~ ^[01]+$ ]] && {
            [[ "$val" == 1 ]] && val=true || val=false
            } || val="'$val'"
    }
    do_cmd
done

### GSETTINGS ###
# org.gnome.desktop.interface avatar-directories @as []
# org.gnome.desktop.interface can-change-accels false
# org.gnome.desktop.interface clock-format '24h'
# org.gnome.desktop.interface clock-show-date true
# org.gnome.desktop.interface clock-show-seconds false
# org.gnome.desktop.interface clock-show-weekday false
# org.gnome.desktop.interface color-scheme 'default'
# org.gnome.desktop.interface cursor-blink true
# org.gnome.desktop.interface cursor-blink-time 1200
# org.gnome.desktop.interface cursor-blink-timeout 10
# org.gnome.desktop.interface cursor-size 24
# org.gnome.desktop.interface cursor-theme 'Adwaita'
# org.gnome.desktop.interface document-font-name 'Cantarell 11'
# org.gnome.desktop.interface enable-animations false
# org.gnome.desktop.interface enable-hot-corners true
# org.gnome.desktop.interface font-antialiasing 'grayscale'
# org.gnome.desktop.interface font-hinting 'slight'
# org.gnome.desktop.interface font-name 'Cantarell 11'
# org.gnome.desktop.interface font-rgba-order 'rgb'
# org.gnome.desktop.interface gtk-color-palette 'black:white:gray50:red:purple:blue:light blue:green:yellow:orange:lavender:brown:goldenrod4:dodger blue:pink:light green:gray10:gray30:gray75:gray90'
# org.gnome.desktop.interface gtk-color-scheme ''
# org.gnome.desktop.interface gtk-enable-primary-paste true
# org.gnome.desktop.interface gtk-im-module ''
# org.gnome.desktop.interface gtk-im-preedit-style 'callback'
# org.gnome.desktop.interface gtk-im-status-style 'callback'
# org.gnome.desktop.interface gtk-key-theme 'Default'
# org.gnome.desktop.interface gtk-theme 'Adwaita'
# org.gnome.desktop.interface gtk-timeout-initial 200
# org.gnome.desktop.interface gtk-timeout-repeat 20
# org.gnome.desktop.interface icon-theme 'Adwaita'
# org.gnome.desktop.interface locate-pointer false
# org.gnome.desktop.interface menubar-accel 'F10'
# org.gnome.desktop.interface menubar-detachable false
# org.gnome.desktop.interface menus-have-tearoff false
# org.gnome.desktop.interface monospace-font-name 'Source Code Pro 10'
# org.gnome.desktop.interface overlay-scrolling true
# org.gnome.desktop.interface scaling-factor uint32 0
# org.gnome.desktop.interface show-battery-percentage false
# org.gnome.desktop.interface text-scaling-factor 1.0
# org.gnome.desktop.interface toolbar-detachable false
# org.gnome.desktop.interface toolbar-icons-size 'large'
# org.gnome.desktop.interface toolbar-style 'both-horiz'
# org.gnome.desktop.interface toolkit-accessibility false

### GTK SETTINGS.INI ###
# gtk-theme-name
# gtk-icon-theme-name
# gtk-font-name
# gtk-cursor-theme-name
# gtk-cursor-theme-size
# gtk-toolbar-style
# gtk-toolbar-icon-size
# gtk-button-images
# gtk-menu-images
# gtk-enable-event-sounds
# gtk-enable-input-feedback-sounds
# gtk-xft-antialias
# gtk-xft-hinting
# gtk-xft-hintstyle
# gtk-xft-rgba
